<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="商品名称">
							<el-input v-model="state.ruleForm.goods" placeholder="请输入商品名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="商品编号">
							<el-input v-model="state.ruleForm.goodsId" placeholder="请输入商品编号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="价格">
							<el-input v-model="state.ruleForm.price" placeholder="请输入价格(kg/元)" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="城市">
							<el-input v-model="state.ruleForm.county" placeholder="请输入城市" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="发布时间">
							<el-date-picker
							v-model="state.ruleForm.date"
							type="datetime"
							placeholder="选择日期时间"
							default-time="12:00:00">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="供应商">
							<el-input v-model="state.ruleForm.provider" placeholder="请输入供应商" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="类型">
							<el-input v-model="state.ruleForm.type" placeholder="请输入类型" clearable></el-input>
						</el-form-item>
					</el-col>
					
					
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="数据状态">
							<el-switch v-model="state.ruleForm.status" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
					
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemUserDialog">
import { ElMessage } from 'element-plus';
import axios from 'axios';
import { startCase } from 'lodash';
import api from "/@/api.js";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		county: '',
		date:'',
		goods:'',
        goodsId:'',
        price:'',
        priceId:'',
        provider:'',
		type:'',
		status:true,
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type, row) => {
	//console.log(row)
	state.dialog.type = type;
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改用户';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增用户';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = {
			county: '',
			date:'2023-05-06T00:00:00.0000',
			goods:'',
			goodsId:'',
			price:'',
			priceId:'',
			provider:'',
			type:'',
			status:true,
		}
	}
	state.dialog.isShowDialog = true;
	
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	

	if(state.dialog.type === 'add'){
		if(state.ruleForm.status){
			state.ruleForm.status = '1';
		}else{
			state.ruleForm.status = '0';
		}
		//alert(state.ruleForm.status);
		// 新增
		axios({
		method: 'post',
		url: api.commonUrl + '/priceData/addpricedata',
		data:state.ruleForm,
	}).then(function (response) {
		if(response.data.code == 200){
			
			ElMessage.success(response.data.message);
		}else{
			ElMessage.error(response.data.message);
			
		}
		emit('refresh');
	});
	}else{
		if(state.ruleForm.status){
			state.ruleForm.status = '1';
		}else{
			state.ruleForm.status = '0';
		}
		// 修改
		axios({
		method: 'post',
		url: api.commonUrl + '/priceData/updatepricedata',
		data:state.ruleForm,
	}).then(function (response) {
		if(response.data.code == 200){
			
			ElMessage.success(response.data.message);
		}else{
			ElMessage.error(response.data.message);
		}
		emit('refresh');
	});
	}
	
	closeDialog();
};


// 暴露变量
defineExpose({
	openDialog,
});
</script>
